{include file="application/admin/view/public/header.html" /}
    <body>
        <div class="x-nav">
            <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" onclick="location.reload()" title="刷新">
                <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
            </a>
        </div>
        <div class="layui-fluid">
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md12">
                    <div class="layui-card">
                        <div class="layui-card-body ">
                            <form class="layui-form layui-col-space5" action="">
                                <div class="layui-input-inline layui-show-xs-block">
                                    <select name="category_id">
                                        <option value="0">选择分类</option>
                                        {volist name='categoryData' id='value'}
                                        <option value="{$value.id}">{$value.name}</option>
                                        {/volist}
                                    </select>
                                </div>
                                <div class="layui-input-inline layui-show-xs-block">
                                    <input type="text" name="name" placeholder="请输入名称" autocomplete="off" class="layui-input">
                                </div>
                                <div class="layui-input-inline layui-show-xs-block">
                                    <input type="text" name="price" placeholder="请输入价格" autocomplete="off" class="layui-input">
                                </div>
                                <div class="layui-input-inline layui-show-xs-block">
                                    <button class="layui-btn" lay-submit="" lay-filter="sreach">
                                        <i class="layui-icon">&#xe615;</i>
                                    </button>
                                </div>
                            </form>
                        </div>
                        <div class="layui-card-header">
                            <a class="layui-btn layui-btn-normal" href="{:url('admin/goods/addGoods')}">
                                <i class="layui-icon"></i>添加
                            </a>
                        </div>
                        <div class="layui-card-body">
                            <table class="table table-bordered">
                                <thead>
                                    <tr>
                                        <th>商品名</th>
                                        <th>一口价</th>
                                        <th>商品封面图</th>
                                        <th>描述</th>
                                        <th>所属分类</th>
                                        <th>上架状态</th>
                                        <th>创建时间</th>
                                        <th>操作</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {volist name='list' id='vo'}
                                    <tr>
                                        <input type="hidden" id='id' value="{$vo.id}">
                                        <td>{$vo.name}</td>
                                        <td>{$vo.price}</td>
                                        <td>
                                            <img src="{$vo.cover_img}" alt="未设置" style="width: 20px;">
                                        </td>
                                        <td>{$vo.description}</td>
                                        <td>{$vo.categoryName}</td>
                                        <td>
                                            {if $vo.status eq 1}
                                            <span style="color: green;" class="td-status">已上架</span>
                                            {else/}
                                            <span style="color: grey;" class="td-status">未上架</span>
                                            {/if}
                                        </td>
                                        <td>{$vo.create_time|date='Y-m-d H:i:s'}</td>
                                        <td>
                                            <a href="{:url('admin/goods/editGoods')}?gid={$vo.id}">查看详情</a> |
                                            {if $vo.status eq 1}
                                            <a href="javascript:;" onclick="member_stop(this,{$vo.id})" title="下架">下架</a> |
                                            {else /}
                                            <a href="javascript:;" onclick="member_stop(this,{$vo.id})" title="上架">上架</a> |
                                            {/if}
                                            <a href="javascript:;" onclick="member_del(this,{$vo.id})">删除</a>
                                        </td>
                                    </tr>
                                    {/volist}
                                </tbody>
                            </table>
                        </div>
                        <div class="layui-card-body ">
                            <div class="page">
                                <div>
                                    {$list|raw}
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </body>
    <script>
        layui.use(['laydate', 'form', 'table'],
        function() {
            var laydate = layui.laydate;
            var form = layui.form;

        });

         //上架下架
          function member_stop(obj,id){
            var msg = $(obj).attr('title');
              layer.confirm('确认要' + msg + '吗？',function(index){
                $.ajax({
                  url:'{:url("admin/goods/ProhibitOrEnable")}',
                  type:'get',
                  dataType:'json',
                  data:'gid=' + id + '&msg=' + msg,
                  success: res => {
                    if (res.code == 1) {
                      if (msg == '下架') {
                        console.log(obj);
                        $(obj).attr('title', '上架')
                        $(obj).html('上架');
                        $(obj).parents("tr").find(".td-status").html('<span style="color: grey;" class="td-status">未上架</span>');
                      } else {
                        $(obj).attr('title', '下架')
                        $(obj).html('下架');
                        $(obj).parents("tr").find(".td-status").html('<span style="color: green;" class="td-status">已上架</span>');
                      }
                      layer.msg(res.msg,{icon: 6,time:1000});
                    } else {
                      layer.msg('失败!',{icon: 5,time:1000});
                    }
                  }
                });
            });
            return false;
        }
        
        function member_del(obj,id){
        layer.confirm('确认要删除吗？该删除会连带相关规格一起删除！！请谨慎操作',function(index){
            $.ajax({
              url:'{:url("admin/goods/delGoods")}',
              type:'get',
              dataType:'json',
              data:'gid=' + id,
              success: res => {
                if (res.code == 1) {
                  $(obj).parents("tr").remove();
                  layer.msg(res.msg, {icon:1, time:1000});
                } else {
                  layer.msg(res.msg, {icon:2, time:1000});
                }
              }
            });
            
        });
        return false;
      }

    </script>

</html>